package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2025-01-16 16:38:22
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{

	@Autowired
	private GraphInfoService graphInfoService;
	@Autowired
	private GraphInfoMapper graphInfoMapper;
	@Autowired
	private LabelInfoMapper labelInfoMapper;
	@Autowired
	private FacilityInfoMapper facilityInfoMapper;
	@Autowired
	private FeeValueMapper feeValueMapper;
	@Autowired
	private RoomInfoMapper roomInfoMapper;
	@Autowired
	private ApartmentFacilityService apartmentFacilityService;
	@Autowired
	private ApartmentLabelService apartmentLabelService;
	@Autowired
	private ApartmentFeeValueService apartmentFeeValueService;
	@Autowired
	private ApartmentInfoMapper apartmentInfoMapper;

	@Override
	public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
		// 判断是更新还是插入
		boolean isUpdate = apartmentSubmitVo.getId() != null;
		// 插入/更新数据
		super.saveOrUpdate(apartmentSubmitVo);
		// 如果是更新则需要删除对应表结构
		if(isUpdate){
			// 删除对应图片表
			LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
			// 添加查询条件：公寓图片信息
			graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
			// 添加查询条件：公寓图片id
			graphQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
			graphInfoService.remove(graphQueryWrapper);

			// 删除对应公寓配套关系
			LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
			// 添加查询条件：公寓id
			apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
			apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

			// 删除对应标签表
			LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
			apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
			apartmentLabelService.remove(apartmentLabelQueryWrapper);

			// 删除对应杂费表
			LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
			apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
			apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
		}

		// 插入对应图片表
		List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
		if (!CollectionUtils.isEmpty(graphVoList)){
			ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
			for (GraphVo graphVo : graphVoList) {
				GraphInfo graphInfo = new GraphInfo();
				graphInfo.setItemType(ItemType.APARTMENT);
				graphInfo.setItemId(apartmentSubmitVo.getId());
				graphInfo.setName(graphVo.getName());
				graphInfo.setUrl(graphVo.getUrl());
				graphInfoArrayList.add(graphInfo);
			}
			graphInfoService.saveBatch(graphInfoArrayList);
		}

		// 插入对应配套表
		// 获取传入的公寓配套id
		List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
		// 判断facilityInfoIds是否为空
		if (!CollectionUtils.isEmpty(facilityInfoIds)){
			// new ArrayList<ApartmentFacility>
			ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
			// 遍历循环取出需要存入的公寓配套id
			for (Long facilityInfoId : facilityInfoIds) {
				// new一个公寓配套id实例
				ApartmentFacility apartmentFacility = new ApartmentFacility();
				// 存入公寓id
				apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
				// 存入设施id
				apartmentFacility.setFacilityId(facilityInfoId);
				// 将公寓配套id实例添入ArrayList中
				apartmentFacilities.add(apartmentFacility);
			}
			// 批量插入对应配套表
			apartmentFacilityService.saveBatch(apartmentFacilities);
		}

		// 插入对应标签表
		// 获取传入的公寓标签id
		List<Long> labelIds = apartmentSubmitVo.getLabelIds();
		// 判断传入的id值是否为空
		if (!CollectionUtils.isEmpty(labelIds)){
			// new ArrayList
			ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
			// for循环将全部的公寓标签id取出
			for (Long labelId : labelIds) {
				// 建立公寓标签关联表实例
				ApartmentLabel apartmentLabel = new ApartmentLabel();
				// 存入ApartmentLabel属性对应的值
				apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
				apartmentLabel.setLabelId(labelId);
				// 将公寓配套id实例添入ArrayList中
				apartmentLabels.add(apartmentLabel);
			}
			// 批量插入对应配套表
			apartmentLabelService.saveBatch(apartmentLabels);
		}


		// 插入对应杂费列表
		List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
		if (!CollectionUtils.isEmpty(feeValueIds)){
			ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
			for (Long feeValueId : feeValueIds) {
				ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
				apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
				apartmentFeeValue.setFeeValueId(feeValueId);
				apartmentFeeValues.add(apartmentFeeValue);
			}
			apartmentFeeValueService.saveBatch(apartmentFeeValues);
		}

	}

	@Override
	public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
		return apartmentInfoMapper.pageItem(apartmentItemVoPage,queryVo);
	}

	@Override
	public ApartmentDetailVo getDetailById(Long id) {
		// 1.查询公寓信息
		ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

		// 2.查询图片表
		List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);

		// 3.查询标签表
		List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

		// 4.查询配套表
		List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

		// 5.查询杂费表
		List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

		// 6.组装结果
		ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
		BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
		apartmentDetailVo.setGraphVoList(graphVoList);
		apartmentDetailVo.setLabelInfoList(labelInfoList);
		apartmentDetailVo.setFacilityInfoList(facilityInfoList);
		apartmentDetailVo.setFeeValueVoList(feeValueVoList);
		return apartmentDetailVo;
	}

	@Override
	public void removeApartmentById(Long id) {
		// 由于公寓下会包含房间信息，因此在删除公寓时最好先判断一下该公寓下是否存在房间信息
		// 若存在，则提醒用户先删除房间信息后再删除公寓信息
		LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
		// 计算房间个数
		Long count = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);
		System.out.println(count);
		if(count>0){
			// 终止删除，并且响应提示信息
			throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
		}

		super.removeById(id);

		// 删除对应图片表
		LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
		// 添加查询条件：公寓图片信息
		graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
		// 添加查询条件：公寓图片id
		graphQueryWrapper.eq(GraphInfo::getId, id);
		graphInfoService.remove(graphQueryWrapper);

		// 删除对应公寓配套关系
		LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
		// 添加查询条件：公寓id
		apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
		apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

		// 删除对应标签表
		LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
		apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
		apartmentLabelService.remove(apartmentLabelQueryWrapper);

		// 删除对应杂费表
		LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
		apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
		apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
	}


}




